﻿using NUnit.Framework;
using Selenium;
using SeleniumToolkit.Config;

namespace SeleniumToolkit.NUnit.Tests
{
 
    /// <summary>
    /// Specifications for the [WebFixture] attribute
    /// </summary>
    public class WebFixtureSpecs : ContextSpecification
    {
        protected ISelenium browser;
        protected Browser browserInfo;

        protected string defaultUrl;
        protected string defaultBrowser;
        protected int defaultTimeout;
        protected bool fixtureOwnsBrowserProcess;

        /// <summary>
        /// Our tests will compare against values defined in the config, read them in before tests execute
        /// </summary>
        protected override void Context_BeforeAllSpecs()
        {
            // Copy config settings to fields for easier assertions

            /* When constructing and running this suite programmatically, the config is
             * not found.  Calling SeleniumConfiguration.GetConfig() in this fashion
             * creates an exception which breaks the programmtic version of this test.
             * Although less than ideal, the config values are stated here.  Some
             * values default from the base config.
            
            var config = SeleniumConfiguration.GetConfig();
            */
            var config = new SeleniumConfiguration();
            config.BrowserUrl = "http://www.google.com";

            defaultUrl = config.BrowserUrl;
            defaultBrowser = config.DefaultBrowser;
            defaultTimeout = config.Timeout;
            fixtureOwnsBrowserProcess = config.AddinSettings.RecylePerFixture;
        }

        protected override void Context()
        {
            // take the current context and assign to local variables for easier access
            browser = Browser.Current;
            browserInfo = Browser.BrowserInfo;
        }

        public class when_fixture_owns_process : WebFixtureSpecs
        {
            protected override void Context_BeforeAllSpecs()
            {
                base.Context_BeforeAllSpecs();

                if (!fixtureOwnsBrowserProcess)
                {
                    Assert.Ignore("This fixture is intended to be run when the fixture controls the browser session.");
                }
                Browser.Current.ShouldNotBeNull();
            }
        }

        

        [WebFixture("*firefox", "http://www.amazon.com", Timeout = 40000)]
        public class when_settings_are_defined_on_the_fixture : when_fixture_owns_process
        {
            protected override void Context_BeforeAllSpecs()
            {
                base.Context_BeforeAllSpecs();

                // make sure the configuration isn't the same as the overridden fixture settings
                defaultUrl.ShouldNotEqual("http://www.amazon.com");
                defaultBrowser.ShouldNotEqual("*firefox");
                defaultTimeout.ShouldNotEqual(4000);
            }


            [WebTest]
            public void test_starts_with_inherited_settings()
            {
                browserInfo.BaseUrl.ShouldEqual("http://www.amazon.com");
                browserInfo.BrowserString.ShouldEqual("*firefox");
                browserInfo.Timeout.ShouldEqual("40000");
            }

            [WebTest]
            public void browser_opens_with_inherited_settings()
            {
                browser.Open("/");
                browser.GetLocation().ShouldEqual("http://www.amazon.com/");
                Pending("need javascript to get user agent");
            }

            [WebTest("*iexplore")]
            public void webtest_with_selenium_browserprofile_is_not_applied()
            {
                browserInfo.BrowserString.ShouldEqual("*firefox");
            }

            [WebTest(BaseUrl = "http://www.delicious.com")]
            public void webtest_with_baseurl_attribute_is_not_applied()
            {
                browserInfo.BaseUrl.ShouldEqual("http://www.amazon.com");
            }

            [WebTest(Timeout = 300)]
            public void webtest_with_timeout_attribute_is_not_applied()
            {
                browserInfo.Timeout.ShouldEqual("40000");
            }

            [WebTest]
            public void run_web_test()
            {
                // do nothing here.
            }

        }

        [WebFixture]
        public class when_webfixture_has_no_settings : when_fixture_owns_process
        {
            [WebTest]
            public void configuration_settings_should_be_used()
            {
                browserInfo.BaseUrl.ShouldEqual(defaultUrl);
                browserInfo.BrowserString.ShouldEqual(defaultBrowser);
                browserInfo.Timeout.ShouldEqual(defaultTimeout.ToString());
            }
        }

    }
}
